package com.henghe.frame.common.util;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2023/9/19 14:53
 */
public class CamelUtil {

    // 下划线字符串转驼峰
    public static String underlineToCamel(String underlineStr) {
        StringBuilder camelStrBuilder = new StringBuilder();
        String[] strArray = underlineStr.split("_");
        for (int i = 0; i < strArray.length; i++) {
            String word = strArray[i];
            if (i == 0) {
                camelStrBuilder.append(word);
            } else {
                camelStrBuilder.append(word.substring(0, 1).toUpperCase());
                camelStrBuilder.append(word.substring(1));
            }
        }
        return camelStrBuilder.toString();
    }

    // 驼峰字符串转下划线
    public static String camelToUnderline(String camelStr) {
        if(StringUtils.isNotEmpty(camelStr) &&camelStr.contains("RESERVATION0"))
            return camelStr;
        if (StringUtils.isNotEmpty(camelStr) && isAllUpperCaseEnglish(camelStr))
            return camelStr;
        StringBuilder underlineStrBuilder = new StringBuilder();
        String[] strArray = camelStr.split("(?=[A-Z])");
        for (int i = 0; i < strArray.length; i++) {
            underlineStrBuilder.append(strArray[i].toLowerCase());
            if (i != strArray.length - 1) {
                underlineStrBuilder.append("_");
            }
        }
        return underlineStrBuilder.toString();
    }

    /**
     * 检查字符串是否全部由大写英文字母组成
     *
     * @param str 需要检查的字符串
     * @return 如果字符串全部由大写英文字母组成，则返回true；否则返回false
     */
    public static boolean isAllUpperCaseEnglish(String str) {
        // 遍历字符串中的每一个字符
        for (int i = 0; i < str.length(); i++) {
            // 如果字符不是大写英文字母，则返回false
            if (!Character.isUpperCase(str.charAt(i))) {
                return false;
            }
        }
        // 如果所有字符都是大写英文字母，则返回true
        return true;
    }

    public static void main(String[] args) {
        System.out.println(camelToUnderline("SYSTEMID"));
    }
}
